package com.neusoft.SPNursingHome.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.SPNursingHome.dao.CareLevelDao;
import com.neusoft.SPNursingHome.dao.CareRecordDao;
import com.neusoft.SPNursingHome.po.*;
import com.neusoft.SPNursingHome.po.CareRecordWithAdminAndCareItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;

@Service
public class CareRecordServiceImpl implements CareRecordService {
    @Autowired
    private CareRecordDao careRecordDao;

    // 查看客户护理记录列表
    @Override
    public ResponseBean<List<CareRecord>> listCareRecord() {
        List<CareRecord> records = careRecordDao.selectActiveRecords();
        return new ResponseBean<>(records);
    }
    // 添加客户护理记录
    @Override
    public ResponseBean<Integer> addCareRecord(CareRecord careRecord) {
        careRecord.setCareRecordStatus(true);
        int result = careRecordDao.insert(careRecord);
        if (result != 1) {
            return new ResponseBean<>(500, "添加客户护理记录失败");
        }
        return new ResponseBean<>(result);
    }

    // 逻辑移除护理记录
    @Override
    public ResponseBean<Integer> deleteCareRecord(Integer careRecordId) {
        if (careRecordId == null) {
            return new ResponseBean<>(400, "护理记录ID不能为空");
        }
        int result = careRecordDao.updateStatusToInactive(careRecordId);
        if (result != 1) {
            return new ResponseBean<>(500, "逻辑删除护理记录状态失败");
        }
        return new ResponseBean<>(result);
    }
    // 根据客户姓名、项目编号、项目名称模糊查询护理记录
    public ResponseBean<List<CareRecord>> getCareRecordByConditions(String clientName, String careItemCode, String careItemName) {
        return null;
    }

    // 管理员选中某个客户可以查看护理记录信息
    // 根据客户id查看客户护理记录列表
    public ResponseBean<List<CareRecordWithAdminAndCareItem>> getCareRecordByClientId(Integer clientId, String careItemName) {
        if (clientId == null) {
            return new ResponseBean<>(400, "客户ID不能为空");
        }
        List<CareRecordWithAdminAndCareItem> records = new ArrayList<CareRecordWithAdminAndCareItem>();
        if (careItemName == null) {
            records = careRecordDao.selectByClientId(clientId);
        }else {
            records = careRecordDao.selectByClientIdAndCareItemName(clientId, careItemName);
        }

        if (records != null) {
            return new ResponseBean<>(records);
        } else {
            return new ResponseBean<>(500, "根据客户id查找客户护理记录失败");
        }
    }
    @Override
    public ResponseBean<IPage<CareRecordWithAdminAndCareItemAndClient>>
    getPageByAdminAndFilters(
            Integer adminId, String careItemName, String careContent, Long page, Long size
    ) {
        if (adminId == null) {
            return new ResponseBean<>(400, "管理员ID不能为空");
        }
        Page<CareRecordWithAdminAndCareItemAndClient> pager = new Page<>(page, size);
        IPage<CareRecordWithAdminAndCareItemAndClient> result =
                careRecordDao.selectPageByAdminAndFilters(pager, adminId, careItemName, careContent);
        return new ResponseBean<>(result);
    }

    @Override
    public ResponseBean<CareRecordWithAdminAndCareItemAndClient> getByRecordId(Integer careRecordId) {
        if (careRecordId == null) {
            return new ResponseBean<>(400, "护理记录ID不能为空");
        }
        CareRecordWithAdminAndCareItemAndClient rec = careRecordDao.selectByRecordId(careRecordId);
        if (rec == null) {
            return new ResponseBean<>(404, "未找到对应的护理记录");
        }
        return new ResponseBean<>(rec);
    }
}
